<template>
    <div class="clock-container">
        <div class="clock">
            <div class="clock-face">
                <!-- 时钟刻度 -->
                <div v-for="n in 12" :key="n" class="hour-mark" :style="{ transform: `rotate(${n * 30}deg)` }">
                    <div class="mark"></div>
                </div>
                <!-- 分钟刻度 -->
                <div v-for="n in 60" :key="`min-${n}`" class="minute-mark" :style="{ transform: `rotate(${n * 6}deg)` }">
                    <div class="mark" v-if="n % 5 !== 0"></div>
                </div>
                <!-- 指针 -->
                <div class="clock-hour" :style="hourStyle"></div>
                <div class="clock-minute" :style="minuteStyle"></div>
                <div class="clock-second" :style="secondStyle"></div>
                <div class="clock-center"></div>
            </div>
        </div>
        <div class="digital-time">{{digitalTime}}</div>
        <div class="date-display">{{currentDate}}</div>
    </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';

// 时钟相关
const digitalTime = ref('');
const currentDate = ref('');
const hourStyle = ref({});
const minuteStyle = ref({});
const secondStyle = ref({});
let timer = null;

const updateClock = () => {
    const now = new Date();
    const hours = now.getHours();
    const minutes = now.getMinutes();
    const seconds = now.getSeconds();
    
    // 更新数字时间
    digitalTime.value = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
    
    // 更新日期
    const year = now.getFullYear();
    const month = now.getMonth() + 1;
    const day = now.getDate();
    const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const weekday = weekdays[now.getDay()];
    currentDate.value = `${year}年${month}月${day}日 ${weekday}`;
    
    // 计算指针角度
    // 时针角度: 30度/小时 + 分钟的影响(30度/60分钟 * 当前分钟)
    const hourDeg = (hours % 12) * 30 + minutes * 0.5;
    // 分针角度: 6度/分钟
    const minuteDeg = minutes * 6 + seconds * 0.1;
    // 秒针角度: 6度/秒
    const secondDeg = seconds * 6;
    
    hourStyle.value = { transform: `rotate(${hourDeg}deg)` };
    minuteStyle.value = { transform: `rotate(${minuteDeg}deg)` };
    secondStyle.value = { transform: `rotate(${secondDeg}deg)` };
};

onMounted(() => {
    // 初始化时钟
    updateClock();
    // 设置定时器每秒更新时钟
    timer = setInterval(updateClock, 1000);
});

onBeforeUnmount(() => {
    // 组件销毁前清除定时器
    if (timer) {
        clearInterval(timer);
    }
});
</script>

<style lang="scss" scoped>
.clock-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.clock {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: linear-gradient(145deg, #f5f7fa, #c3cfe2);
    border: 3px solid #409EFF;
    position: relative;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.1), 
                inset 0 0 15px rgba(255, 255, 255, 0.5),
                0 0 0 1px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    
    &:hover {
        transform: scale(1.05);
        box-shadow: 0 0 20px rgba(64, 158, 255, 0.4),
                    inset 0 0 15px rgba(255, 255, 255, 0.5);
    }
}

.clock-face {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

// 时刻度
.hour-mark {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    
    .mark {
        position: absolute;
        width: 2px;
        height: 8px;
        background-color: #333;
        left: 49px;
        top: 0;
    }
}

// 分刻度
.minute-mark {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    
    .mark {
        position: absolute;
        width: 1px;
        height: 3px;
        background-color: #999;
        left: 49.5px;
        top: 0;
    }
}

.clock-center {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 8px;
    height: 8px;
    margin: -4px 0 0 -4px;
    background: radial-gradient(circle, #409EFF, #1867c0);
    border-radius: 50%;
    z-index: 4;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.4);
}

.clock-hour {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 3px;
    height: 30px;
    margin: -30px 0 0 -1.5px;
    background-color: #303133;
    transform-origin: bottom center;
    z-index: 1;
    border-radius: 3px 3px 0 0;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
}

.clock-minute {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: 40px;
    margin: -40px 0 0 -1px;
    background-color: #606266;
    transform-origin: bottom center;
    z-index: 2;
    border-radius: 2px 2px 0 0;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
}

.clock-second {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1px;
    height: 45px;
    margin: -45px 0 0 -0.5px;
    background-color: #F56C6C;
    transform-origin: bottom center;
    z-index: 3;
    border-radius: 1px 1px 0 0;
    box-shadow: 0 0 2px rgba(255, 0, 0, 0.3);
}

.digital-time {
    margin-top: 10px;
    font-size: 18px;
    color: #409EFF;
    font-weight: bold;
    letter-spacing: 1px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.date-display {
    margin-top: 5px;
    font-size: 12px;
    color: #606266;
}
</style>






